package com.czxy.service;

import com.czxy.dao.UserMapper;
import com.czxy.domain.User;
import com.czxy.utils.MyBatisUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

public class UserService {

    // 把地址为北京的人的性别  改为女

    public  void  updateByInfo(User user){

        UserMapper mapper = MyBatisUtils.getMapper(UserMapper.class);

        Example example = new Example(User.class);

        Example.Criteria ct = example.createCriteria();
        ct.andEqualTo("address",user.getAddress());

        mapper.updateByExampleSelective(user,example);

        MyBatisUtils.commitAndClose();




    }


















    //把地址 为  指定地址的数据 删掉

    public  void  delByInfo(User user){


        UserMapper mapper = MyBatisUtils.getMapper(UserMapper.class);

        Example example = new Example(User.class);
        Example.Criteria ct = example.createCriteria();

        ct.andEqualTo("address",user.getAddress());

        mapper.deleteByExample(example);

        MyBatisUtils.commitAndClose();

    }














    public  List<User> findUserByInfo(User user){
        UserMapper mapper = MyBatisUtils.getMapper(UserMapper.class);

        Example example = new Example(User.class);

        Example.Criteria ct = example.createCriteria();

        ct.andLike("username","%"+user.getUsername()+"%");


        ct.andEqualTo("sex",user.getSex());

        List<User> users = mapper.selectByExample(example);

        MyBatisUtils.close();

        return users;


    }




















    //更新

    public  void  update(User user){
        UserMapper mapper = MyBatisUtils.getMapper(UserMapper.class);

        mapper.updateByPrimaryKey(user);

        MyBatisUtils.commitAndClose();
    }


    //更新

    public  void  updateSelective(User user){
        UserMapper mapper = MyBatisUtils.getMapper(UserMapper.class);
        mapper.updateByPrimaryKeySelective(user);

        MyBatisUtils.commitAndClose();
    }

























    //根据 uid 删除

    public  void  del (Integer uid){
        UserMapper mapper = MyBatisUtils.getMapper(UserMapper.class);

        int i = mapper.deleteByPrimaryKey(uid);

        MyBatisUtils.commitAndClose();

        System.out.println("受影响的行数 "+  i);



    }








    //添加

    public  void add (User user){
        UserMapper mapper = MyBatisUtils.getMapper(UserMapper.class);

        int i = mapper.insert(user);


        MyBatisUtils.commitAndClose();
        System.out.println("受影响的行数 "+  i);

    }





    //分页查询

    public PageInfo findFy(int pageNum, int pageSize){

        UserMapper mapper = MyBatisUtils.getMapper(UserMapper.class);

        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        //查询
        List<User> users = mapper.selectAll();
        //封装
        PageInfo<User> pageInfo = new PageInfo<>(users);

        MyBatisUtils.close();

        return pageInfo;

    }




    //根据主键查询

    public  User findUid(User user){

        UserMapper mapper = MyBatisUtils.getMapper(UserMapper.class);

        User u = mapper.selectByPrimaryKey(user);

        MyBatisUtils.close();


        return u;
    }









    //查询所有

    public List<User> findAll(){

        UserMapper mapper = MyBatisUtils.getMapper(UserMapper.class);

        List<User> list = mapper.selectAll();

        MyBatisUtils.close();

        return list;
    }






}
